package com.cm.study.spring.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Service;

import java.util.Arrays;

/**
 * 1.标记为这是一个切面
 * 2.需要给那些地方进行切面
 * 3.切入的类型
 * 		1.方法执行前
 * 		2.方法执行后
 * 		3.方法返回后
 * 		4.方法异常后
 * 4.不管是切面还是被切的都需要spring管理
 */
//@Aspect
//@Service
public class AopAspect1 {
	@Pointcut("execution(* com.cm.study.spring.aop..*.*(..))")
	public void pointCut() {}
	
	@Before(value = "pointCut()")
	public void before(JoinPoint joinPoint) {
		System.out.println("AopAspect1 before方法执行");
	}
	@After(value = "pointCut()")
	public void after(JoinPoint joinPoint) {
		System.out.println(" AopAspect1after方法执行");
	}
	@AfterReturning(value = "pointCut()",returning = "obj")
	public void afterReturning(JoinPoint joinPoint,Object obj) {
		Object[] args = joinPoint.getArgs();
		System.out.println("AopAspect1 afterReturning方法执行,参数："+Arrays.asList(args)+",返回值:"+obj);
	}
	@AfterThrowing(value = "pointCut()",throwing="ex")
	public void afterThrowing(JoinPoint joinPoint,Exception ex) {
		Object[] args = joinPoint.getArgs();
		System.out.println("AopAspect1 afterThrowing方法执行,参数："+Arrays.asList(args)+",异常为:"+ex.getMessage());
	}
	//@Around(value = "pointCut()")
	public void afterAround(ProceedingJoinPoint jp) {
		Object[] args = jp.getArgs();
		System.out.println("AopAspect1 Around方法执行,参数："+Arrays.asList(args));
	}
}
